package com.efast.cafe.portal.controller.company;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.efast.cafe.portal.bean.company.PortalCompanyPositionRoleBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.LanguageUtil;
import com.efast.cafe.portal.bean.base.BaseCompanyInfoBean;
import com.efast.cafe.portal.bean.company.PortalCompanyOrgBean;
import com.efast.cafe.portal.bean.company.PortalCompanyPositionQueryForm;
import com.efast.cafe.portal.entity.company.PortalCompanyOrg;
import com.efast.cafe.portal.entity.company.PortalCompanyPosition;
import com.efast.cafe.portal.entity.company.PortalCompanyUserReporttoTreeDto;
import com.efast.cafe.portal.service.base.IBaseCompanyInfoService;
import com.efast.cafe.portal.service.company.IPortalCompanyOrgService;
import com.efast.cafe.portal.service.company.IPortalCompanyPositionService;

@Controller
@RequestMapping("/frame_center/company")
public class PortalCompanyPositionController extends AbstractController{
	@Autowired
	private IPortalCompanyOrgService portalCompanyOrgService;
	@Autowired
	private IBaseCompanyInfoService baseCompanyInfoService;
	@Autowired
	private IPortalCompanyPositionService portalCompanyPositionService;
	
	
	
	
	/**
	 * 获取当前公司结构树
	 * @Date        :     
	 * @return
	 */
		@RequestMapping(value = "/position/tree")
		public @ResponseBody ResultBean getCompanyTree(
				HttpSession session,@RequestBody Map map){
//		public @ResponseBody ResultBean getCompanyTree(
//				HttpSession session)throws JSONException{
//			Map map = new HashMap();
//			map.put("titleFilter", null);
			ResultBean result=new ResultBean(true,"");
			BaseCompanyInfoBean companyInfo= baseCompanyInfoService.queryByCode();
			PortalCompanyUserReporttoTreeDto dto=new PortalCompanyUserReporttoTreeDto();
			dto.setId("org_"+0);
			dto.setParentId("company_"+LanguageUtil.getValueByJsonLocale(companyInfo.getCompany_name()));
			dto.setName(LanguageUtil.getValueByJsonLocale(companyInfo.getCompany_name()));
			dto.setIconSkin("company");
			dto.setIsHidden(false);
			dto.setType("company");
//			List<PortalCompanyUserReporttoTreeDto>  data=portalCompanyUserReporttoService.getCompanyTree();
			List<PortalCompanyUserReporttoTreeDto>  data = portalCompanyPositionService.getCompanyTree();
			data.add(dto);
			List<PortalCompanyOrgBean> listResult = new ArrayList<>();
			for (int i = 0; i < data.size(); i++) {
				PortalCompanyOrgBean portalCompanyOrgBean1 = new PortalCompanyOrgBean();
				portalCompanyOrgBean1.setTitle(data.get(i).getName());
				portalCompanyOrgBean1.setData(data.get(i));
				listResult.add(portalCompanyOrgBean1);
			}
			result.setObject(listResult);
			return result;
		}
	
		
		/**
		 * 获取当前公司结构树
		 * @Date        :     
		 * @return
		 */
			@RequestMapping(value = "/position/lowerlist")
			public @ResponseBody ResultBean getLowerList(
					ModelMap model,HttpServletRequest request,HttpServletResponse response
					,@RequestBody PortalCompanyUserReporttoTreeDto thisDto
					){
				ResultBean result=new ResultBean(true,"");
//				PortalCompanyUserReporttoTreeDto thisDto = new PortalCompanyUserReporttoTreeDto();
//				thisDto.setId("org_287");
//				thisDto.setParentId("org_0");
//				thisDto.setName("系统管理员");
//				thisDto.setType("org");
//				thisDto.setLevel("user");
				List<PortalCompanyUserReporttoTreeDto>  data = portalCompanyPositionService.getLowerList(thisDto);
				List<PortalCompanyOrgBean> listResult = new ArrayList<>();
				for (int i = 0; i < data.size(); i++) {
					PortalCompanyOrgBean portalCompanyOrgBean1 = new PortalCompanyOrgBean();
					portalCompanyOrgBean1.setTitle(data.get(i).getName());
					portalCompanyOrgBean1.setData(data.get(i));
					listResult.add(portalCompanyOrgBean1);
				}
				result.setObject(listResult);
				return result;
			}	
		
		
		
		
	
	
	
	/**
	 * 跳转到list页面
	 * @Date        :      2016-8-16
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/position/treePosition")
	public @ResponseBody ResultBean infoPage(ModelMap model,
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestBody Map<String,Integer> map){
		ResultBean result=new ResultBean(true, "");
		
		 Integer portal_company_org_id=null;
		 if(map.containsKey("id")){
			 portal_company_org_id=map.get("id");
		 }
		
			BaseCompanyInfoBean companyInfo= baseCompanyInfoService.queryByCode();
			PortalCompanyOrg portalCompanyOrg=new PortalCompanyOrg();
			portalCompanyOrg.setPortal_company_org_id(0L);
			portalCompanyOrg.setOrg_name(companyInfo.getCompanyName_zh_CN());
			List<PortalCompanyOrgBean> listResult=new ArrayList<>();
			PortalCompanyOrgBean portalCompanyOrgBean=new PortalCompanyOrgBean();
			portalCompanyOrgBean.setData(portalCompanyOrg);
        	portalCompanyOrgBean.setTitle(portalCompanyOrg.getOrg_name());
        	listResult.add(portalCompanyOrgBean);
        	result.setObject(listResult);
			
		
		
			List<PortalCompanyOrg> listpoPortalCompanyOrgList = portalCompanyOrgService.queryPortalCompanyOrgList();	
            for(int i=0;i<listpoPortalCompanyOrgList.size();i++){
            	PortalCompanyOrgBean portalCompanyOrgBean1=new PortalCompanyOrgBean();
            	portalCompanyOrgBean1.setTitle(listpoPortalCompanyOrgList.get(i).getOrg_name());
            	portalCompanyOrgBean1.setData(listpoPortalCompanyOrgList.get(i));
            	listResult.add(portalCompanyOrgBean1);
            }
            result.setObject(listResult);
		return result;
	}
	
	/**
	 * 通过orgId异步查询position
	 * @Date        :      2016-8-16
	 * @param model
	 * @param request
	 * @param portalCompanyOrgId
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/position/list")
	public @ResponseBody ResultBean queryPositionByOrgId(ModelMap model,
			HttpServletRequest request,
			@RequestParam(required = false) Long portal_company_org_id,
			HttpServletResponse response){
		ResultBean result=new ResultBean(true, "");
		List<PortalCompanyPosition> listpCompanyPositions = portalCompanyPositionService.queryPositionByOrgId(portal_company_org_id);
		result.setObject(listpCompanyPositions);
		
		return result;
	}
	
	
	/**
	 * 分页展示position
	 * @Date        :      2016-8-16
	 * @param request
	 * @param response
	 * @param start
	 * @param length
	 * @param portalCompanyOrgId
	 * @param portalCompanyPosition
	 * @return
	 * @throws JSONException
	 */
	@RequestMapping(value = "/position/listPosition")
	public @ResponseBody ResultBean portalparameterList(
			HttpServletRequest request,
			@RequestBody PortalCompanyPositionQueryForm portalCompanyPosition){
		
		ResultBean result=new ResultBean(true, "");
		if(portalCompanyPosition.getPortal_company_org_id()!=null&&portalCompanyPosition.getPortal_company_org_id()==0){
			portalCompanyPosition.setPortal_company_org_id(null);
		}
		result.setObject(portalCompanyPositionService.querylistPosition(portalCompanyPosition));
		return result;
	}
	
	/**
	 * 通过id删除position
	 * @Date        :      2016-8-16
	 * @param request
	 * @param response
	 * @param portalCompanyOrgId
	 * @return
	 */
	@RequestMapping(value = "/position/delete")
	public @ResponseBody ResultBean delPosutionById(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody Map<String,Integer> map 
			) {
		Long portalCompanyPositionId=null;
		if(map.containsKey("portal_company_position_id")){
			portalCompanyPositionId=Long.parseLong(map.get("portal_company_position_id").toString());
		}
		ResultBean result = new ResultBean(true, null);
		try {
			int count =  portalCompanyPositionService.listUserByPositionId(portalCompanyPositionId);
			if (count != 0) {
				throw new ServiceException("职位含有用户不能删除");
			}
			portalCompanyPositionService.delPosutionById(portalCompanyPositionId);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
		
	}
	
	
	
	
	
	@RequestMapping(value = "/position/edit")
	public @ResponseBody ResultBean updatePosution(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody PortalCompanyPositionRoleBean portalCompanyPosition
			) {
		
		ResultBean result=new ResultBean(true,"");
		try{
			portalCompanyPositionService.updatePosition(portalCompanyPosition);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(e.getMessage());
		}
	return result;
		
	}
	
	
	@RequestMapping(value = "/position/add")
	public @ResponseBody ResultBean insertPosutionById(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody PortalCompanyPositionRoleBean portalCompanyPosition
			) {
		
		ResultBean result=new ResultBean(true,"");
		try{

			portalCompanyPositionService.insertPosition(portalCompanyPosition);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(e.getMessage());
		}
	return result;
		
	}
}
